/*
 * Created on Feb 17, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ModuleBuilder.UI;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import ModuleBuilder.model.*;
import java.util.*;
import javax.swing.BorderFactory; 
import javax.swing.border.Border;

/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class TerrainTileTray extends JPanel {
	static HashMap lookup = new HashMap();

	
	  BuilderTrayModel model;
	  // JComboBox trayNameCombo;
	  JList list;
	  DefaultListModel listMod;
	  Dimension size = new Dimension (ImagePreview._previewIconSize+20,ImagePreview._previewIconSize+20);
	  Vector currSubTray;
	  // TerrainTrayView  currTv;
		TerrainTileTray thisTray;
	  // Integer id;

	  public TerrainTileTray() {
		// super(name);
		// Container cont = this.getContentPane();
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		// trayNameCombo = new JComboBox();
		list = new JList();
		list.setCellRenderer(new ComboBoxRenderer() );
		list.setBorder(BorderFactory.createLoweredBevelBorder());
		// this.add(trayNameCombo);
		this.add(Box.createRigidArea(new Dimension(0,3)));
		JScrollPane scrollMap = new JScrollPane(list);
		this.add(scrollMap);
		// thisTray = this;
		// lookup.put(idIn, this);
	  }

	  class ComboBoxRenderer extends JLabel implements ListCellRenderer {
		  public ComboBoxRenderer() {
			  setOpaque(true);
			  setHorizontalAlignment(LEFT);
			  setVerticalAlignment(CENTER);
			  setVerticalTextPosition(SwingConstants.BOTTOM);
			  setHorizontalTextPosition(SwingConstants.CENTER);
		  }
		  public Component getListCellRendererComponent(
			  JList list,
			  Object value,
			  int index,
			  boolean isSelected,
			  boolean cellHasFocus)
		  {
		  	 Color bc=null;
			  if (isSelected) {
			  	bc = list.getSelectionBackground();
				  setBackground(bc);
				  setForeground(list.getSelectionForeground());
				  
			  } else {
				bc = list.getBackground();
				  setBackground(bc);
				  setForeground(list.getForeground());
			  }
				ImageElement uv= (ImageElement)value;
			  ImageIcon icon = uv.getSmallIcon();
			  // !!! SHOULD check if u can first get the old border, then reset it to save mem 
			  Border b =  BorderFactory.createMatteBorder(5, 5, 5, 5, bc);
			  this.setBorder(b);
			  setIcon(icon);
			  return this;
		  }
	  }
	  class ListSelectionHandler implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			if (e.getValueIsAdjusting() == false) {
			  //String name = thisTray.getName();
			  int index = list.getSelectedIndex();
			  if ( index > -1 ) {
				// Debug.out("Terrain index=" + index + " was selected");
				// currTv = (TerrainTrayView)currSubTray.elementAt(index);
				ImageElement el = model.getImageAt(index);
				// stick the selected el in the diction ary
				Utils.lookup.put(Utils._TERRAIN_SELECTED_FROM_TRAY, el);
				// Util.lookup.put(Util._CURR_SELECTED_TRAY, thisTray);
				// MouseHandler mh = (MouseHandler)Util.lookup.get(Util._CURR_MOUSE_HANDLER);
				// mh.setElement( el );
			  }
			}
		}
	  }

	  public Dimension getPreferredSize() {
		return ( size );
	  }

	  public Dimension getMinimumSize() {
		return ( size );
	  }

	  public void setModel ( BuilderTrayModel m ) {
		model = m;
		// now setup the combobox and list from the model
//		Set trayNameSet = model.getAllTrayNames();
//		Object[] trayNameArray = trayNameSet.toArray();
//		int arraySize = trayNameArray.length;
//		for ( int i=0; i< arraySize; i++ ) {
//		  String currName = (String)trayNameArray[i];
//		  trayNameCombo.addItem(currName);
//		}
//		if (arraySize > 0) {
//		  String selectedTrayName = (String)trayNameArray[0];
//		  trayNameCombo.setSelectedItem(selectedTrayName);
//		  currSubTray = model.getTrayByName(selectedTrayName);
//		  list.setListData(currSubTray);
//		}
		ArrayList modelList = model.getList();
		Object[] terrainElementArray = modelList.toArray();
		list.setListData(terrainElementArray);
		size = list.getPreferredSize();

		// setup action handler to listen to which subtray is selected
//		trayNameCombo.addActionListener(new ActionListener() {
//		   public void actionPerformed(ActionEvent e) {
//			  JComboBox cb = (JComboBox)e.getSource();
//			  String selectedTrayName = (String)cb.getSelectedItem();
//			  currSubTray = model.getTrayByName(selectedTrayName);
//			  list.setListData(currSubTray);
//			}
//		 });

		// setup a list action listen to listen to which unit within a tray
		// is selected
		//
		// DefaultListSelectionModel selMod = list.getSelectionModel();
		list.addListSelectionListener(new ListSelectionHandler());
		// this.pack();
	  }

	  /**
	   * The current selected view is removed and the SubTray is updated
	   * (THIS SHOULD NOT BE USED SINCE TERRAIN DO NOT GO AWAY WHEN PLACED)
	   */
	  public void removeSelectElement() {
//		//System.out.println("removeSelectElement() fired");
//		currSubTray.removeElement(currTv);
//		Util.lookup.put(Util._EL_SELECTED_FROM_TRAY, null);
//		list.setListData(currSubTray);
//		currTv = null;
//		list.repaint();
	  }
  
//	  public void removeSelectElement( Vector subTray, TerrainTrayView  utv ) {
//		//System.out.println("removeSelectElement() fired");
//		subTray.removeElement(utv);
//		if ( currTv == utv ) {
//			// someone just removed the currentTv
//			currTv = null;
//			Element el = (Element)Util.lookup.get(Util._EL_SELECTED_FROM_TRAY);
//			if ( el.equals(utv.getElement())){
//				Util.lookup.put(Util._EL_SELECTED_FROM_TRAY, null);
//			}
//		}
//		list.setListData(subTray);
//		list.repaint();
//	  }
  
//	  public Vector getSubTrayFromUnitId ( Integer id ) {
//		// first search through all the trays 
//		Set trayNameSet = model.getAllTrayNames();
//		Object[] trayNameArray = trayNameSet.toArray();
//		int arraySize = trayNameArray.length;
//		TerrainTrayView utv = null;
//		Vector t = null;
//		for ( int i=0; i< arraySize; i++ ) {
//		  String currName = (String)trayNameArray[i];
//		  // now we have a name of the tray. now we get the tray
//		  t = model.getTrayByName(currName);
//		  // now go through all the units
//		  for (int j=0; j < t.size(); j++) {
//			utv = (TerrainTrayView)t.elementAt(j);
//			Element el = utv.getElement();
//			if (id.equals(el.getElementId())) {
//				// we found a matching element Id
//				return (t); 
//			}
//		  }
//		}
//		return ( t ); 	
//	  }
	  /**
	   * This Routine will search through the entire tray, and then try to return the TerrainTrayView 
	   * No data structure is changed in this case
	   * @param id The element Id in question
	   * @return return the matching TerrainTrayView
	   */
//	  public TerrainTrayView getTerrainTrayViewFromElementId( Integer id, Vector t ) {
//		TerrainTrayView utv = null;
//		for (int j=0; j < t.size(); j++) {
//			utv = (TerrainTrayView)t.elementAt(j);
//			Element el = utv.getElement();
//			if (id.equals(el.getElementId())) {
//				// we found a matching element Id
//				return (utv); 
//			}
//		}
//		return ( utv );
//	  }
//  
//	  public Integer getId() {
//		return ( id );
//	  }
//  
//		public static void put( Integer trayIdIn, Tray t) {
//			lookup.put(trayIdIn, t);
//		}
//  
//		public static Tray get( Integer trayIdIn ) {
//			return ( (Tray)lookup.get(trayIdIn));
//		}
}
